package com.example.news_demo.utils;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import com.example.news_demo.entity.Content;
import com.example.news_demo.service.ArticleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;


@Component
public class TestJieBa {
    @Autowired
    private ArticleService articleService;

    // private Map<String, Double> idfmap = new HashMap<String, Double>();
    private HashSet<String> stopWordsSet;

    public void qu() {
        if (stopWordsSet == null) {
            stopWordsSet = new HashSet<>();
            new TFIDFAnalyzer().loadStopWords(stopWordsSet, this.getClass().getResourceAsStream("/static/stop_words.txt"));
        }
    }



    public void set_idf() throws IOException {
        System.out.println("开始计时：");
        long startTime = System.currentTimeMillis();
        List<Content> list = articleService.selectContent();
        System.out.println(list);
        long d = (System.currentTimeMillis() - startTime) / 1000;
        long minute = d / 60;
        long second = d % 60;
        System.out.println("读取数据完成，耗时：" + minute + "分" + second + "秒");

        long startTime2 = System.currentTimeMillis();
        JiebaSegmenter segmenter = new JiebaSegmenter();
        Map<String, Integer> idfmap2 = new HashMap<String, Integer>();
        int  op= list.size();
        for (int i = 0; i < list.size(); i++) {

            List<SegToken> tokenList = segmenter.process(list.get(i).getContent(), JiebaSegmenter.SegMode.SEARCH);
            // HashSet<String> mamap = new HashSet<String>();
            HashMap<String, Integer> tempMap = new HashMap<>();

            for (SegToken token : tokenList) {
                // 遍历列表，从列表中每个SegToken对象中，获得word。
                System.out.println(token.word);
                if (!stopWordsSet.contains(token.word) && token.word.length() > 1) {
                    //mamap.add(token.word);
                    if(!tempMap.containsKey(token.word)){
                        tempMap.put(token.word, 1);
                    }else {
                        Integer count = tempMap.get(token.word);
                        count = count+1;
                        tempMap.put(token.word, count);
                    }
                }
            }
            Set<String> set = tempMap.keySet();
            for (String s:set){
                System.out.println(s+"----"+tempMap.get(s));
            }



            for (String s:set){
                if(idfmap2.containsKey(s)){
                    int z = idfmap2.get(s);
                    z++;
                    idfmap2.put(s,z);
                }else {
                    idfmap2.put(s, tempMap.get(s));
                }
            }


        }


        d = (System.currentTimeMillis() - startTime2) / 1000;
        minute = d / 60;
        second = d % 60;
        System.out.println("分词1000条完成，耗时：" + minute + "分" + second + "秒");
        String path = "F:\\desktop\\1.txt";
        File file = new File(path);
        if(!file.exists()){
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);


        String key = null;
        double value ;
        // 获取键集合的迭代器
        Iterator it = idfmap2.keySet().iterator();
        while (it.hasNext()) {
            key = (String) it.next();
            //value = Math.log((double)op/((Integer) idfmap2.get(key)+1));
            value = idfmap2.get(key);
            bw.write(key+" "+value);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        fw.close();

    }






}
